<!DOCTYPE html>
<html>
<head>
    <title>登录页面</title>
    <meta charset="utf-8"/>
    <link rel="stylesheet" type="text/css" href="common.css">
    <script type="text/javascript" src="common.js"></script>
    <script>
        const baseUrl = "";
        window.onload = function () {
            // 注册按钮
            $("_doRegister").onclick = function () {
                let email = $("email").value;
                let reg = new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
                if (!reg.test(email)) {
                    alert("不符合邮箱地址格式");
                    return;
                }
                let password = $("password").value;
                let confirmPwd = $("confirmPwd").value;
                let verifyCode = $("verifyCode").value;
                if (checkNotEmpty(email, "邮箱") && checkNotEmpty(password, "密码") &&
                    checkNotEmpty(confirmPwd, "确认密码") && checkNotEmpty(verifyCode, "验证码")) {
                    if (password !== confirmPwd) {
                        alert("两次密码不一致");
                        return;
                    }
                    const registerDTO = {
                        "email": email,
                        "password": password,
                        "confirmPwd": confirmPwd,
                        "verifyCode": verifyCode
                    };
                    ajax({
                        type: "POST",
                        url: baseUrl + "/user/register",
                        dataType: "json",
                        data: registerDTO,
                        success: function (data) {
                            let result = checkResult(data);
                            if (result["success"]) {
                                window.open("login.html");
                            }
                        }
                    });
                }
            };
            let timer;
            let lock = false;
            // 获取验证码按钮
            $("_getVerifyCode").onclick = function () {
                if (lock) {
                    return;
                }
                let email = $("email").value;
                if (!checkNotEmpty(email, "邮件")) {
                    return;
                }
                let reg = new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
                if (!reg.test(email)) {
                    alert("不符合邮箱地址格式");
                    return;
                }
                let sec = 60;
                lock = true;
                $("_getVerifyCode").innerText = sec + "s后重试";
                clearInterval(timer);
                timer = setInterval(function () {
                    sec--;
                    if (sec <= 0) {
                        clearInterval(timer);
                        $("_getVerifyCode").innerText = "获取验证码";
                        lock = false;
                    } else {
                        $("_getVerifyCode").innerText = sec + "s后重试";
                        lock = true;
                    }
                }, 1000);
                // TODO:获取验证码
                ajax({
                    type: "POST",
                    url: baseUrl + "/create/" + email,
                    success: function (data) {
                        let result = checkResult(data);
                        if (result["success"]) {

                        }
                    }
                });
            };
        }
    </script>
</head>
<body>
<ul class="content">
    <li>邮 箱：<input type="text" id="email"/></li>
    <li>密 码：<input type="password" id="password"/></li>
    <li>确认密码：<input type="password" id="confirmPwd"/></li>
    <li>验证码：<input type="text" id="verifyCode"/><a id="_getVerifyCode">获取验证码</a></li>
    <li><a id="_doRegister">注 册</a></li>
</ul>
</body>
</html>